/**
This file is part of Gel2DE.

    Gel2DE is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Gel2DE is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Gel2DE.  If not, see <http://www.gnu.org/licenses/>.

	Copyright (C) 2012, University of Bergen
*/

#include "SpearmanRankData.h"

SpearmanRankData::SpearmanRankData(int aNelements)
{
	m_data = new std::vector<SpearmanRankElement*>();
	m_data->reserve(aNelements);
	m_nbrXties = new std::vector<int>();
	m_nbrYties = new std::vector<int>();
	m_nbrXties->reserve(aNelements);
	m_nbrYties->reserve(aNelements);
}

SpearmanRankData::~SpearmanRankData(void)
{
	std::vector<SpearmanRankElement*>::iterator i = m_data->begin();
	for(i ; i != m_data->end(); i++)
	{
		delete (*i);
	}
	m_data->clear();
	delete m_data;

	m_nbrXties->clear();
	m_nbrYties->clear();

	delete m_nbrXties;
	delete m_nbrYties;
}


SpearmanRankData::SpearmanRankData(std::vector<double>* aXvector, std::vector<double>* aYvector)
{
	m_data = new std::vector<SpearmanRankElement*>();
	if(aXvector->size() != aYvector->size())
	{
		std::cerr << " Error: Dimension of input vectors is not the same" << std::endl;
	}
	else
	{
		std::vector<double>::const_iterator iterx;
		std::vector<double>::const_iterator itery;
		iterx = aXvector->begin();
		itery = aYvector->begin();

		while(iterx != aXvector->end())
		{
			SpearmanRankElement *e = new SpearmanRankElement(*iterx, *itery);
			m_data->push_back(e);
			iterx++;
			itery++;
		}
		m_nbrXties = new std::vector<int>();
		m_nbrYties = new std::vector<int>();

	}
}

void SpearmanRankData::Clear()
{
	m_nbrXties->clear();
	m_nbrYties->clear();
	for(int i = 0; i < m_data->size(); i++)
	{
		delete (*m_data)[i];
	}
	m_data->clear();
}

void SpearmanRankData::RankData()
{
	Rank(SpearmanRankElement::X);
	Rank(SpearmanRankElement::Y);
}

bool SortOnX(SpearmanRankElement* a, SpearmanRankElement* b)
{
	return a->GetValue(SpearmanRankElement::X) > b->GetValue(SpearmanRankElement::X);
}

bool SortOnY(SpearmanRankElement* a, SpearmanRankElement* b)
{
	return a->GetValue(SpearmanRankElement::Y) > b->GetValue(SpearmanRankElement::Y);
}

void SpearmanRankData::Rank(int aRef)
{
	//Sort on x
	if(aRef == SpearmanRankElement::X)
		std::sort(m_data->begin(), m_data->end(), SortOnX);
	else
		std::sort(m_data->begin(), m_data->end(), SortOnY);

	std::vector<SpearmanRankElement*>::iterator i = m_data->begin();
	std::vector<SpearmanRankElement*>::iterator j = m_data->begin();

	int rank = 1;
	int nextRank;

	std::vector<int> ties;

	//Calculate the rank:
	//Loop over the array:
	while(i != m_data->end())
	{
		j = i;
		j++; //Move j to next element
		int numberOfEqualElements = 0;
		nextRank = rank + 1;
		//If a tie is detected, move j to the end of the tie and count number
		//of elements:
		while(j != m_data->end() && (*i)->GetValue(aRef) == (*j)->GetValue(aRef))
		{
			numberOfEqualElements++;
			j++;
			nextRank++;
		}
		//If a tie was found for this i:
		if(numberOfEqualElements > 0)
		{
			float meanRank = (nextRank - 1 + rank)/2.0f;
			//Hand out the ranks for the tied data:
			for(i; i != j; i++)
			{
				(*i)->SetRank(aRef, meanRank);
			}
			rank = nextRank;
			i = j;
			//Store the number of elements in this tie, including the first element.
			m_nbrXties->push_back(numberOfEqualElements + 1);
		}
		//Assign the same rank to all these elements
		else
		{
			(*i)->SetRank(aRef, (float)rank);
			rank++;
			i++;
		}
	}
}

std::vector<SpearmanRankElement*>* SpearmanRankData::GetData()
{
	return m_data;
}

std::vector<int>* SpearmanRankData::GetXties()
{
	return m_nbrXties;
}
std::vector<int>* SpearmanRankData::GetYties()
{
	return m_nbrYties;
}

void SpearmanRankData::AddElement(double aX, double aY)
{
	SpearmanRankElement *e = new SpearmanRankElement(aX, aY);
	m_data->push_back(e);
}

void SpearmanRankData::AddElements(std::vector<double>* aX, std::vector<double>* aY)
{
	for( int i = 0; i < aX->size(); i++)
	{
		m_data->push_back(new SpearmanRankElement((*aX)[i], (*aY)[i]));
	}
}
